package cn.demo.mq.work.rr;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Mr.Still
 * @version 1.0.0
 * @date 2021-10-18 15:25
 **/
public class Receiver01 {

    public static String QUEUE_NAME = "work_rr";
    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("8.129.129.84");
            factory.setPort(5672);
            factory.setVirtualHost("/demo");
            factory.setUsername("user");
            factory.setPassword("password");
            //创建连接对象
            connection = factory.newConnection();
            //创建通道
            channel = connection.createChannel();

            channel.queueDeclare(QUEUE_NAME,false,false,false,null);

            Consumer consumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    String message = new String(body,"utf-8");
                    System.out.println("01接收到消息-》》》》》"+message);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            //队列名，true 消息收到进行删除 ，consumer
            //监听指定队列
            channel.basicConsume(QUEUE_NAME,true,consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
